package com.fivetalks.network;

import java.io.UnsupportedEncodingException;
import java.util.Vector;

import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;


public class DataSender extends Thread{
//	private final String URL =  "http://10.201.208.180:8080/myproject/test";
	private final String URL =  "http://192.168.0.27:8080/myproject/member/join";
	private Vector<BasicNameValuePair> nameValue;
	
	public DataSender(Vector<BasicNameValuePair> nameValue)
	{
		this.nameValue = nameValue;
	}
	
	@Override
    public void run(){
    	try{
    		String result;
			result = sendData(nameValue,URL);
			System.out.println(result);
    	}catch (Exception e){
				e.printStackTrace();
			}
    }
    public String sendData(Vector<BasicNameValuePair> nameValue, String url) throws Exception {

		HttpPost request = makeHttpPost(nameValue, url);

		HttpClient client = new DefaultHttpClient();
		ResponseHandler<String> reshandler = new BasicResponseHandler();
		String result = client.execute(request, reshandler);
		
		return result;

	}
    
	private HttpPost makeHttpPost(Vector<BasicNameValuePair> nameValue, String url) throws Exception {
		HttpPost request = new HttpPost(url);
 
		request.setEntity(makeEntity(nameValue));

		return request;
	}

	private HttpEntity makeEntity(Vector<BasicNameValuePair> nameValue) throws Exception {
		HttpEntity result = null;
		try {
			result = new UrlEncodedFormEntity(nameValue,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}

}
